package com.github.binarywang.demo.spring.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import io.renren.dao.WeixinUserDao;
import io.renren.entity.WeixinUser;
import io.renren.service.WeixinUserService;
import io.renren.utils.Config;
import io.renren.utils.Page;


/**
 * 微信用户
 * 
 * @author zk
 * @email zk13899@163.com
 * @date 2017-07-18 16:36:24
 **/
@Controller
@RequestMapping("/Weixin")
public class WeixinContrller {
	@Autowired
	private WeixinUserService weixinUserService;
	@Autowired
	private WeixinUserDao weixinUserDao;
	
	 @RequestMapping(value="user.html")
	    public String list(Model model,String openid ) {
		 System.out.println(openid+"-----");
		 model.addAttribute("openid",openid);
	//	  model.addAttribute("path",Config.getPath());
			return "user_register";
		  
	  }
	 
	
	 @RequestMapping(value="save")
	 public String regstrer(HttpServletRequest request, Model model){
		 model.addAttribute("path",Config.getPath());
		 String nickname=request.getParameter("nickname");
		 String openid=request.getParameter("openid");
		 String sex=request.getParameter("sex");
		 String phone=request.getParameter("phone");
		 
		System.out.println(nickname+"==========openid="+openid);
		 WeixinUser user=new WeixinUser();
		 user.setNickname(nickname);
		 user.setOpenid(openid);
		 user.setPhone(phone);
		 user.setSex(Integer.parseInt(sex));
		 weixinUserDao.save(user);
		 
		 return "succeed";
		 
	 }
	      /**
	       * 用户列表
	       * @param nickname
	       * @param req
	       * @param model
	       * @return
	       */
		   @RequestMapping("/lists")
		   public String lists(String nickname,HttpServletRequest req ,Model model){
	    	 model.addAttribute("path",Config.getPath());
			Map<String, Object> map = new HashMap<>();
			map.put("nickname", nickname);
		    String page1=req.getParameter("pageNo");
		   
		   if(page1==null){
			   page1="1";
		   } 
			
			int pageNo =Integer.valueOf(page1);
		 
			int pageSize =2;
			map.put("offset", (pageNo - 1) * pageSize);
			map.put("limit", pageSize);
			List<WeixinUser> weixinUserList = weixinUserService.queryList(map);
			int totalCount=weixinUserService.queryTotal(map);
	
			//查询列表数据	// 分页
			
			Page page = new Page();
			page.setPageNo(pageNo);
			int totalPage = 0;
			
			if (totalCount % pageSize == 0) {
				totalPage = (int) (totalCount / pageSize);
			} else {
				totalPage = (int) (totalCount / pageSize) + 1;
			}
			model.addAttribute("totalPage", totalPage);
			model.addAttribute("page", page);
			
			
			model.addAttribute("list",weixinUserList );
			//分页
			return "user_list";
		}
		   
			/**
			 * 信息
			 */
			@RequestMapping("/info/{id}")
			public String info(@PathVariable("id") Integer id,HttpServletRequest req,Model model){
				model.addAttribute("path",Config.getPath());
				System.out.println("我来这里了++++++");
				WeixinUser weixinUser = weixinUserService.queryObject(id);
				req.setAttribute("weixinUser", weixinUser);
				return "user_update";
			}
		   /**
			 * 修改
			 */
			@RequestMapping("/update")
			public String update(WeixinUser weixinUser, HttpServletRequest req, Model model){
				model.addAttribute("path",Config.getPath());
				weixinUserService.update(weixinUser);
				
				Map<String, Object> map = new HashMap<>();
				List<WeixinUser> weixinUserList = weixinUserService.queryList(map);
				model.addAttribute("list",weixinUserList );
				
				return "user_list";
			}
		   
 }